<?php

namespace app\admin\controller;

use think\Controller;
use Classes\PHPExcel_IOFactory;
use Classes\PHPExcel;

class Index extends Controller
{
    public $log_file = 'zd.txt';

    public function readfile(){
        $fileName = "zd.xlsx";
        if (!file_exists($fileName)) {
            exit("文件".$fileName."不存在");
        }

        vendor("PHPExcel.Classes.PHPExcel.PHPExcel");
        vendor("PHPExcel.Classes.PHPExcel.Writer.IWriter");
        vendor("PHPExcel.Classes.PHPExcel.Writer.Abstract");
        vendor("PHPExcel.Classes.PHPExcel.IOFactory");
        vendor("PHPExcel.Classes.PHPExcel.Writer.Excel5");
        vendor("PHPExcel.Classes.PHPExcel.Writer.Excel2007");

        $objPHPExcel = \PHPExcel_IOFactory::load($fileName);

        //获取sheet表格数目
        // $sheetCount = $objPHPExcel->getSheetCount();

        //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected);

        //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow();
        //获取表格列数
        // $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        $dataArr = array();

        // 循环读取每个单元格的数据
        //行数循环
        for ($row = 2; $row <= $rowCount; $row++){
            $dataArr[$row-2]['questionNo']= (int)$objPHPExcel->getActiveSheet()->getCell("A".$row)->getValue();
            $dataArr[$row-2]['questionTitle']= $objPHPExcel->getActiveSheet()->getCell("B".$row)->getValue();
            $dataArr[$row-2]['questionOption']= $objPHPExcel->getActiveSheet()->getCell("C".$row)->getValue();
            $dataArr[$row-2]['answer']= $objPHPExcel->getActiveSheet()->getCell("D".$row)->getValue();
            $dataArr[$row-2]['questionType']= (int)$objPHPExcel->getActiveSheet()->getCell("F".$row)->getValue();
        }
        // $dataArr['2']['topic']= $objPHPExcel->getActiveSheet()->getCell("E6")->getValue();
        return $dataArr;
        // echo "<pre>";
        // dump($dataArr);
        // echo "</pre>";
    }

    public function index(){
        $mobile = action('readfile');
        // dump($mobile);
        foreach($mobile as $v){
            // dump($v);
            $data[]=[
                'questionNo' => $v['questionNo'],
                'questionTitle' => $v['questionTitle'],
                'questionOption' => $v['questionOption'],
                'answer' => $v['answer'],
                'questionType' => $v['questionType'],
                'state'=>0
            ];
        }
        $db_inset = db('q_bank')->insertAll($data);
        dump($db_inset);
    }

    public function demo()
    {
        $res = db('q_user')->where("userToken = '76e74c53a3ef39e9bc5a0e3dbacddd5f'")->setInc('totalScore');
        dump($res);
    }
}
